﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

/**Class: Game_Setup
 * @Author Jayson Park, Samuel McCravy
 * @version 0.99d
 * Course       : ITEC 4860 Spring 2012
 * Written      : March 20th, 2012
 * Last Revised : April 20th, 2012
 * 
 * This class allows a user to create a game using a specified gameset or by a random picking from a certain subcategory.
 * 
 * Purpose: Create a game based on existing game set
 *          Create a game based on subcategory.
 * */

namespace quizzer_linq_3
{
    public partial class Game_Setup : Form
    {
        //This is used to lookup main ID's

        const int MAIN_ID_LOOKUP_INDEX = 0;

        //This is used to access the database

        Quizzer_Data_Access dataAccess = new Quizzer_Data_Access();

        //When pulling from the QDB using getQuestion methods, this is the index for the 
        //question ID when splitting the returned string using the '|' delimiter.

        const int QUIZZERDB_QUESTION_QUESTIONID_INDEX = 0;

        //When pulling from the QDB using getQuestion methods, this is the index for the 
        //subcategory ID when splitting the returned string using the '|' delimiter.

        const int QUIZZERDB_QUESTION_SUBCATEGORYID_INDEX = 1;

        //When pulling from the QDB using getCategory methods, this is the index for the 
        //category id when splitting the returned string using the '|' delimiter.

        const int QUIZZERDB_CATEGORY_CATEGORYID_INDEX = 0;

        //When pulling from the QDB using getCategory methods, this is the index for the 
        //category name when splitting the returned string using the '|' delimiter.

        const int QUIZZERDB_CATEGORY_CATEGORYNAME_INDEX = 1;

        //When pulling from the QDB using getCategoryForSubcategory methods, this is the index for the 
        //subcategory id when splitting the returned string using the '|' delimiter.

        const int QUIZZERDB_CAT_SUBCAT_SUBCATEGORYID_INDEX = 0;

        //When pulling from the QDB using getCategoryForSubcategory methods, this is the index for the 
        //subcategory name when splitting the returned string using the '|' delimiter.

        const int QUIZZERDB_CAT_SUBCAT_SUBCATEGORY_NAME_INDEX = 1;

        //When pulling from the QDB using getCategoryForSubcategory methods, this is the index for the 
        //category id when splitting the returned string using the '|' delimiter.

        const int QUIZZERDB_CAT_SUBCAT_CATEGORYID_INDEX = 2;

        //Used to keep track of all the Category IDs within the QCSubcategoryComboBox

        private List<String> questionSubCategoryIDList = new List<String>();

        public Game_Setup()
        {
            InitializeComponent();
            //Populates the combo boxes
            populateGSCategoryComboBox();
            defaultCategoryComboBox();
            createGameFromSetRadio.Select();
            enterSetNumberBox.Select();
        }

        /**Method: defaultCategoryComboBox
         * @author Jayson Park
         * Sets the default index for category combo box so the form controls are not blank when showing the form the first time
         */

        private void defaultCategoryComboBox()
        {
            if (GSCategoryComboBox.Items.Count > 0)
            {
                GSCategoryComboBox.SelectedIndex = 0;
            }
        }

        /** Method: exitButton_Click
         * @author Samuel McCravy
         * Closes the form
         * 
         * @param sender sender of the action
         * @param e event is triggered
        */

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /** Method: generateButton_Click
         * Checks to see if the program needs to create a game from a game set or randomly from a subcategory
         * 
         * @param sender sender of the action
         * @param e event is triggered
        */

        private void generateButton_Click(object sender, EventArgs e)
        {
            if (createGameFromSetRadio.Checked)
            {
                gameSetGame();
            }
            else
            {
                subcategoryGameCheck();
            }
        }

        /** Method: gameSetGame
         * @author Jayson Park
         * Starts the game, checking the gameset name and sending it to Quizzer
        */

        private void gameSetGame()
        {
            if (checkValidGameSet(enterSetNumberBox.Text))
            {
                String gameSetString = enterSetNumberBox.Text;
                this.Hide();
                Quizzer gameForm = new Quizzer(gameSetString);
                gameForm.ShowDialog();
                this.Show();
            }
            else
            {
                MessageBox.Show("Invalid Game Set ID", "Error!");
            }
        }

        /** Method: subcategoryGameCheck
         * @author Jayson Park
         * Checks if there is a blank category or subcategory selected before starting the game
        */

        private void subcategoryGameCheck()
        {
            if (GSCategoryComboBox.Items.Count < 1)
            {
                MessageBox.Show("There are no categories!", "Error!");
            }
            else if (GSSubCategoryComboBox.Items.Count < 1)
            {
                MessageBox.Show("There are no subcategories for this category", "Error!");
            }
            else
            {
                subcategoryGame();
            }
        }

        /** Method: subcategoryGame
         * @author Jayson Park
         * Starts the game, picking questions randomly from a subcategory and sending it to Quizzer
        */

        private void subcategoryGame()
        {
            string[] questions = dataAccess.GetAllQuestion().Trim().Split('\r');
            string[] question;
            List<String> questionIDs = new List<String>();
            int categoryID = Convert.ToInt32(dataAccess.GetCategory(((string)GSCategoryComboBox.SelectedItem).Trim()).Split('|').GetValue(QUIZZERDB_CATEGORY_CATEGORYID_INDEX));
            string subcategoryID = (string)dataAccess.GetSubCategoryForCategory(categoryID).Split('|').GetValue(QUIZZERDB_CAT_SUBCAT_SUBCATEGORYID_INDEX);

            for (int questionCounter = 0; questionCounter < questions.Length; questionCounter++)
            {
                question = questions[questionCounter].Split('|');
                if (question.GetValue(QUIZZERDB_QUESTION_SUBCATEGORYID_INDEX).Equals(subcategoryID))
                {
                    questionIDs.Add((String)question.GetValue(QUIZZERDB_QUESTION_QUESTIONID_INDEX));
                }
            }

            this.Hide();
            Quizzer gameForm = new Quizzer((string[])questionIDs.ToArray());
            gameForm.ShowDialog();
            this.Show();

        }

        /** Method: checkDuplicateAdd
         * @author Jayson Park
         * Checks if the question was already added into the game set.
         * 
         * @param questionID the question ID number of the question to add
         * @param setID the ID number of the set to add the question to
         * @return a boolean flag dependent on whether a duplicate question was found
        */

        private Boolean checkValidGameSet(string setName)
        {
            string[] gameSets = (((string)dataAccess.GetAllGameSet()).Trim()).Split('\r');
            string gameSetCheck = "";
            for (int gameSetCounter = 0; gameSetCounter < gameSets.Length; gameSetCounter++)
            {
                gameSetCheck = (string)gameSets[gameSetCounter].Split('|').GetValue(1);
                if (setName.Equals(gameSetCheck))
                {
                    return (true);
                }
            }
            return (false);
        }

        /**Method: populateGSCategoryComboBox
         * @author Jayson Park
         * This populates the Category Combobox with all of the categories from the database.
         * Sets the current category to the first category added to the combo box.
         */

        private void populateGSCategoryComboBox()
        {
            GSCategoryComboBox.Items.Clear();

            String[] categories = dataAccess.GetAllCategory().Split('\n');

            for (int categoryCounter = 0; categoryCounter < categories.Length - 1; categoryCounter++)
            {
                GSCategoryComboBox.Items.Add(((String)(categories.GetValue(categoryCounter))).Split('|').GetValue(QUIZZERDB_CATEGORY_CATEGORYNAME_INDEX));
            }
            int currentCategoryID;
            if (GSCategoryComboBox.Items.Count > 0)
            {
                GSCategoryComboBox.SelectedIndex = 0;
                currentCategoryID = Convert.ToInt32(dataAccess.GetCategory(((string)GSCategoryComboBox.SelectedItem).Trim()).
                    Split('|').GetValue(QUIZZERDB_CATEGORY_CATEGORYID_INDEX));
            }
            else
            {
                currentCategoryID = 0;
            }
            populateGSSubCategoryComboBox(currentCategoryID);
        }

        /**Method: populateGSSubCategoryComboBox
         * @author Jayson Park
         * This populates the Subcategory Combobox with all of the Subcategories from the related Category.
         * Sets the current Subcategory to the first Subcategory added to the combo box.
         */

        private void populateGSSubCategoryComboBox(int currentCategory)
        {
            GSSubCategoryComboBox.Items.Clear();
            questionSubCategoryIDList.Clear();

            String[] subCategories = dataAccess.GetSubCategoryForCategory(currentCategory).Split('\n');

            for (int subCategoryCounter = 0; subCategoryCounter < subCategories.Length - 1; subCategoryCounter++)
            {
                GSSubCategoryComboBox.Items.Add(((String)(subCategories.GetValue(subCategoryCounter))).Split('|').GetValue(QUIZZERDB_CAT_SUBCAT_SUBCATEGORY_NAME_INDEX));
                questionSubCategoryIDList.Add((String)((String)(subCategories.GetValue(subCategoryCounter))).Split('|').GetValue(QUIZZERDB_CAT_SUBCAT_SUBCATEGORYID_INDEX));
            }
            if (GSSubCategoryComboBox.Items.Count > 0)
            {
                GSSubCategoryComboBox.SelectedIndex = 0;
            }
        }

        /** Method: enterSetNumberBox_Enter
         * When the enterSetNumberBox is focused, select all of the text.
         * 
         * @param sender sender of the action
         * @param e event is triggered
        */

        private void enterSetNumberBox_Enter(object sender, EventArgs e)
        {
            enterSetNumberBox.SelectAll();
        }

        /**Method: GSCategoryComboBox - SelectedIndexChanged
         * 
         * This is an auto generated method by Visual Studio
         * 
         * This method checks if the GSCategoryComboBox has been changed in order to set the changesSaved flag to false 
         * This also calls to the populateGSSubCategoryComboBox with the current category id in order for the 
         * GSSubCategoryComboBox to successfully update the subcategories with the given selected category.
         */

        private void GSCategoryComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (GSSubCategoryComboBox.Items.Count > 0)
            {
                populateGSSubCategoryComboBox(Convert.ToInt32(dataAccess.GetCategory(((string)GSCategoryComboBox.SelectedItem).Trim()).Split('|').GetValue(0)));
            }
            else
            {
                populateGSSubCategoryComboBox(0);
            }
        }
    }
}
